<template>
  <el-drawer
      title="添加"
      :visible.sync="visible"
      :before-close="handleClose"
      :destroy-on-close="true"
      :wrapperClosable="false"
      :size="this.drawerSize"
  >
    <div class="drawer-content">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="href">
          <el-input v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.qOrder"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="form.showFlag"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">确定</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<script>
import { DRAWER_SIZE_BIG } from "@/util/constant";
import { insertQuickApi } from "@/api/modules/websites/quick.api";

export default {
  data() {
    return {
      visible: false,
      form: {
        name: "",
        href: "",
        qOrder: 1,
        showFlag: true,
      },
      drawerSize: DRAWER_SIZE_BIG,
      rules: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        href: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  methods: {
    init() {
      this.visible = true;
    },
    handleClose() {
      this.visible = false;
    },
    handleAdd() {
      this.$refs.form.validate(async (valid) => {
        if(valid){
          await insertQuickApi(this.form);
          this.$message({
            message: "成功",
            type: "success",
          });
          this.form = {
            name: "",
            href: "",
            qOrder: 1,
            showFlag: true,
          };
          this.visible = false;
          this.$emit("getParentTableData");
        }
      })
    },
  },
};
</script>
